import org.apache.spark.SparkConf
import org.apache.spark.rdd.RDD
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.streaming.{Seconds, StreamingContext}


object Transform {

  def main(args: Array[String]): Unit = {

    val sparkConf = new SparkConf().setMaster("local[*]").setAppName(" ")
    val ssc = new StreamingContext(sparkConf, Seconds(5))

    // 在Driver端执行，全局一次,main线程
    println("driver 1......:" + Thread.currentThread().getName)

    ssc.socketTextStream("usdp-o3tbdsfp-monitor1", 4444)
      .transform( //转换为rdd

        rdd => {
          // 在Driver端执行 (JobGenerator线程)，一个批次一次
          println("driver 2......:" + Thread.currentThread().getName)

          val result: RDD[(String, Int)] = rdd
            .flatMap(_.split(" "))
            .map(x => {

              // 在Executor端执行，和数据个数相同
              println("executor......:" + Thread.currentThread().getName)
              (x, 1)
            })
            .reduceByKey(_ + _)

          result
        }
      )
      .print

    ssc.start()
    ssc.awaitTermination()
  }
}

